import { _decorator, Component, instantiate, Node, Prefab, SpriteFrame } from 'cc';
import { JoyStickManager } from '../UI/JoyStickManager';
import DataManager from '../Global/DataManager';
import { ResourceManager } from '../Global/ResourceManager';
import { ActorManager } from '../Entity/Actor/ActorManager';
import { PrefabPathEnum, TexturePathEnum } from '../Enum';
import { EntityTypeEnum } from '../Common/Enum';
const { ccclass, property } = _decorator;
//场景管理类
@ccclass('BattleManager')
export class BattleManager extends Component {
    //舞台
    private stage: Node
    //UI
    private ui: Node
    //资源加载是否成功
    private shouldUpdate: Boolean = false

    onLoad() {
        this.stage = this.node.getChildByName("Stage")
        this.ui = this.node.getChildByName("UI")
        //销毁所有场景对象，因为之前有的都要删除
        this.stage.destroyAllChildren()
        //查询底下所有子组件，找出摇杆并赋值
        DataManager.Instance.jm = this.ui.getComponentInChildren(JoyStickManager)
    }

    async start(): Promise<void> {
        //先加载预制体
        await this.loadRes()
        this.initMap()
        this.shouldUpdate = true
    }

    //异步加载
    async loadRes() {
        const list = []
        for (const type in EntityTypeEnum) {
            //加载角色背景预制体
            const p = ResourceManager.Instance.loadRes(PrefabPathEnum[type], Prefab).then((prefab) => {
                DataManager.Instance.prefabMap.set(type, prefab)
            })
            list.push(p)
        }

        for (const type in TexturePathEnum) {
            //加载帧动画图片
            const p = ResourceManager.Instance.loadDir(TexturePathEnum[type], SpriteFrame).then((spriteFrame) => {
                DataManager.Instance.textureMap.set(type, spriteFrame)
            })
            list.push(p)
        }

        await Promise.all(list)
    }

    initMap(){
        //实例化地图
        const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Map)
        const map = instantiate(prefab)
        //把创建设置为父节点，等于是进入舞台了
        map.setParent(this.stage)
    }

    update(dt: number): void {
        if (!this.shouldUpdate) {
            return
        }
        //如果是资源加载成功，才进入帧刷新方法
        this.render()
        this.tick(dt)//每帧检测摇杆变化
    }

    tick(dt: number){
        this.tickActor(dt)
    }

    //调用移动
    tickActor(dt: number){
        for (const data of DataManager.Instance.state.actors) {
            const { id } = data
            //读出场景上有角色，再每帧执行获取他们的信息
            let am = DataManager.Instance.actorMap.get(id)
            am.tick(dt)
        }
    }

    //每帧动起来
    render() {
        //移动角色、子弹等等
        this.renderActor()
    }

    //处理角色
    async renderActor() {
        //遍历所有角色
        for (const data of DataManager.Instance.state.actors) {
            const { id, type } = data
            let am = DataManager.Instance.actorMap.get(id)
            //判断是否存在
            if (!am) {
                //如果不存在就实例化它
                const prefab = DataManager.Instance.prefabMap.get(type)
                const actor = instantiate(prefab)
                //把创建设置为父节点，等于是进入舞台了
                actor.setParent(this.stage)
                //为自己动态添加脚本
                am = actor.addComponent(ActorManager)
                //放入到map集合中
                DataManager.Instance.actorMap.set(data.id, am)
                //初始化一下自己
                am.init(data)
            } else {
                //下一帧执行render
                am.render(data)
            }
        }
    }

}


